package org.jeecg.modules.recruit.position.service.impl;

import org.jeecg.modules.recruit.position.entity.MmCompanyPosition;
import org.jeecg.modules.recruit.position.entity.MmCompanyPositionItem;
import org.jeecg.modules.recruit.position.entity.MmCompanyPicture;
import org.jeecg.modules.recruit.position.entity.MmCompanyLabel;
import org.jeecg.modules.recruit.position.mapper.MmCompanyPositionItemMapper;
import org.jeecg.modules.recruit.position.mapper.MmCompanyPictureMapper;
import org.jeecg.modules.recruit.position.mapper.MmCompanyLabelMapper;
import org.jeecg.modules.recruit.position.mapper.MmCompanyPositionMapper;
import org.jeecg.modules.recruit.position.service.IMmCompanyPositionService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 职位信息
 * @Author: jeecg-boot
 * @Date:   2020-08-24
 * @Version: V1.0
 */
@Service
public class MmCompanyPositionServiceImpl extends ServiceImpl<MmCompanyPositionMapper, MmCompanyPosition> implements IMmCompanyPositionService {

	@Autowired
	private MmCompanyPositionMapper mmCompanyPositionMapper;
	@Autowired
	private MmCompanyPositionItemMapper mmCompanyPositionItemMapper;
	@Autowired
	private MmCompanyPictureMapper mmCompanyPictureMapper;
	@Autowired
	private MmCompanyLabelMapper mmCompanyLabelMapper;
	
	@Override
	@Transactional
	public void saveMain(MmCompanyPosition mmCompanyPosition, List<MmCompanyPositionItem> mmCompanyPositionItemList,List<MmCompanyPicture> mmCompanyPictureList,List<MmCompanyLabel> mmCompanyLabelList) {
		mmCompanyPositionMapper.insert(mmCompanyPosition);
		if(mmCompanyPositionItemList!=null && mmCompanyPositionItemList.size()>0) {
			for(MmCompanyPositionItem entity:mmCompanyPositionItemList) {
				//外键设置
				entity.setPositonId(mmCompanyPosition.getId());
				mmCompanyPositionItemMapper.insert(entity);
			}
		}
		if(mmCompanyPictureList!=null && mmCompanyPictureList.size()>0) {
			for(MmCompanyPicture entity:mmCompanyPictureList) {
				//外键设置
				entity.setPositonId(mmCompanyPosition.getId());
				mmCompanyPictureMapper.insert(entity);
			}
		}
		if(mmCompanyLabelList!=null && mmCompanyLabelList.size()>0) {
			for(MmCompanyLabel entity:mmCompanyLabelList) {
				//外键设置
				entity.setParentId(mmCompanyPosition.getId());
				mmCompanyLabelMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(MmCompanyPosition mmCompanyPosition,List<MmCompanyPositionItem> mmCompanyPositionItemList,List<MmCompanyPicture> mmCompanyPictureList,List<MmCompanyLabel> mmCompanyLabelList) {
		mmCompanyPositionMapper.updateById(mmCompanyPosition);
		
		//1.先删除子表数据
		mmCompanyPositionItemMapper.deleteByMainId(mmCompanyPosition.getId());
		mmCompanyPictureMapper.deleteByMainId(mmCompanyPosition.getId());
		mmCompanyLabelMapper.deleteByMainId(mmCompanyPosition.getId());
		
		//2.子表数据重新插入
		if(mmCompanyPositionItemList!=null && mmCompanyPositionItemList.size()>0) {
			for(MmCompanyPositionItem entity:mmCompanyPositionItemList) {
				//外键设置
				entity.setPositonId(mmCompanyPosition.getId());
				mmCompanyPositionItemMapper.insert(entity);
			}
		}
		if(mmCompanyPictureList!=null && mmCompanyPictureList.size()>0) {
			for(MmCompanyPicture entity:mmCompanyPictureList) {
				//外键设置
				entity.setPositonId(mmCompanyPosition.getId());
				mmCompanyPictureMapper.insert(entity);
			}
		}
		if(mmCompanyLabelList!=null && mmCompanyLabelList.size()>0) {
			for(MmCompanyLabel entity:mmCompanyLabelList) {
				//外键设置
				entity.setParentId(mmCompanyPosition.getId());
				mmCompanyLabelMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		mmCompanyPositionItemMapper.deleteByMainId(id);
		mmCompanyPictureMapper.deleteByMainId(id);
		mmCompanyLabelMapper.deleteByMainId(id);
		mmCompanyPositionMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			mmCompanyPositionItemMapper.deleteByMainId(id.toString());
			mmCompanyPictureMapper.deleteByMainId(id.toString());
			mmCompanyLabelMapper.deleteByMainId(id.toString());
			mmCompanyPositionMapper.deleteById(id);
		}
	}
	
}
